package com.heima.article.task;

import com.heima.article.service.ApHotArticlesService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 在每天凌晨一点进行文章算分的任务
 *
 * @author Fuqiang
 * @since :2023-04-02
 */
@Slf4j
@Component
public class CalculateArticleScoreTask {


    @Autowired
    ApHotArticlesService hotArticlesService;

    /**
     * 定时计算Article分值
     *
     * @param days 要计算的天数(从今天往前计算)
     * @return
     * @throws Exception
     */
    @XxlJob("calculateScoreJobHandler")
    public ReturnT<String> calculateArticleScoreHandler(String days) throws Exception {
        // 使用这种方式打的日志会记录到 Xxl-Job的日志中
        XxlJobLogger.log("calculateArticleScoreHandler init");
        log.info("开始同步Redis中的数据到MySQL...");
        hotArticlesService.redisDataToDB();

        hotArticlesService.calculateArticleScore(Long.parseLong(days));

        return ReturnT.SUCCESS;
    }


}
